<template>
    <el-container class="home-container">
        <!-- 页头 -->
        <el-header>
            <div>
                <img src="../assets/logo.jpg" alt>
                <span>智慧农业仓库信息管理系统</span>
            </div>
        <!-- 退出按钮 -->
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        
        
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'" :collapse-transition="false">
                <!-- 切换按钮 -->
                <div class="toggle-Button" @click="toggleCollapse">|||</div>
                <!-- 菜单 -->
                <el-menu
                    background-color="#545c64"
                    text-color="#fff"
                    active-text-color="#409eff"
                    unique-opened
                    :collapse="isCollapse"
                    :collapse-transition = "false"
                    :router="true"
                    :default-active="activePath"
                    >
                    <!-- 一级菜单项 -->
                    <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
                        <template #title>
                            <i :class="iconsObject[item.id]"></i>
                            <span>{{ item.title }}</span>
                        </template>
                        <!-- 二级菜单项 -->
                        <el-menu-item :index="it.path+''" v-for="it in item.sList" :key="it.id" @click = "saveNavState(it.path)">
                            <template #title>
                                <i :class="iconsObject[it.id]"></i>
                                <span>{{ it.title }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 主内容区域 -->
            <el-main>
                <router-view>

                </router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            // 菜单列表
            menuList: [],
            // 侧边栏折叠状态
            isCollapse: false,
            
            iconsObject: {
                
            },
            activePath:'/welcome',
            

        };
    },
    created() {
        // 初始化时获取菜单列表
        this.getMenuList();
        this.activePath = window.sessionStorage.getItem('activePath');
    },
    methods: {
        // 退出登录
        logout() {
            window.sessionStorage.clear();
            this.$router.push('/login');
        },
        // 获取菜单列表
        async getMenuList() {
            const { data: res } = await this.$http.get("menus");
            if (res.flag != 200) return this.$message.error('获取菜单失败');
            this.menuList = res.menus;
        },
        // 切换侧边栏折叠状态
        toggleCollapse() {
            this.isCollapse = !this.isCollapse;
        },
        //保存路径
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath);
            this.activePath = activePath;
        }
    }
};
</script>

<style lang="less" scoped>
// 容器样式
.home-container {
    height: 100%;
}
// 页头样式
.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0%;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div {
        display: flex;
        align-items: center;
        span {
            margin-left: 15px;
        }
    }
}
// 侧边栏样式
.el-aside {
    background-color: #333744;
    .el-menu {
        border-right: none;
    }
}
// 主内容区域样式
.el-main {
    background-color: #eaedf1;
}
// 图片样式
img {
    width: 55px;
    height: 55px;
}
// 切换按钮样式
.toggle-Button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>